package com.overseasShopping.Action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.overseasShopping.Biz.UserBiz;
import com.overseasShopping.pojo.User;

public class UserDetailAction extends ActionSupport{
	
	private File myimg;
	private String signature;
	private String nickName;
	private String myimgContentType ;
	private String myimgFileName ;
	private UserBiz userBiz;
	
	public void setMyimg(File myimg) {
		this.myimg = myimg;
	}

	public String getMyimgContentType() {
		return myimgContentType;
	}

	public void setMyimgContentType(String myimgContentType) {
		this.myimgContentType = myimgContentType;
	}

	public String getMyimgFileName() {
		return myimgFileName;
	}

	public void setMyimgFileName(String myimgFileName) {
		this.myimgFileName = myimgFileName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}
	
	public UserBiz getUserBiz() {
		return userBiz;
	}

	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}

	public String submitImg() throws Exception {
		Map<String, Object>map=ActionContext.getContext().getSession();
		User user=(User) map.get("user");
		String suf=myimgFileName.substring((myimgFileName.lastIndexOf(".")));
		String pathname=ServletActionContext.getServletContext().getRealPath("/")+"uImg/"+user.getUserId()+suf;
		map.put("imgSuf", suf);
		File file=new File(pathname);
		FileOutputStream os=new FileOutputStream(file);
		FileInputStream is=new FileInputStream(myimg);
		byte[] bytes=new byte[is.available()];
		is.read(bytes);
		is.close();
		os.write(bytes);
		os.flush();
		os.close();
		user.getDetailUser().setUimage(Hibernate.createBlob(bytes));
		userBiz.updateUser(user);
		return SUCCESS;
	}
	
	//修改签名
	public String signature() throws Exception {
		Map<String, Object>map=ActionContext.getContext().getSession();
		User user=(User) map.get("user");
		user.getDetailUser().setSignature(signature);
		userBiz.updateUser(user);
		return null;
	}
	//修改昵称
		public String resetnickName() throws Exception {
			Map<String, Object>map=ActionContext.getContext().getSession();
			User user=(User) map.get("user");
			user.getDetailUser().setNickName(nickName);
			userBiz.updateUser(user);
			User newuser = userBiz.getUser(user.getUserId());
			map.put("user", newuser);
			return null;
		}
}
